package org.dromara.system.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.PayPolicyVo;
import org.dromara.system.domain.bo.PayPolicyBo;
import org.dromara.system.service.IPayPolicyService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 风控策略
 * 前端访问路由地址为:/system/PayPolicy
 *
 * @author Gavino
 * @date 2024-12-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/PayPolicy")
public class PayPolicyController extends BaseController {

    private final IPayPolicyService payPolicyService;

    /**
     * 查询风控策略列表
     */
    @SaCheckPermission("system:PayPolicy:list")
    @GetMapping("/list")
    public TableDataInfo<PayPolicyVo> list(PayPolicyBo bo, PageQuery pageQuery) {
        return payPolicyService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询风控策略列表
     */
    @GetMapping("/selectList")
    public R<?> selectList(PayPolicyBo bo) {
        return R.ok(payPolicyService.queryList(bo));
    }

    /**
     * 导出风控策略列表
     */
    @SaCheckPermission("system:PayPolicy:export")
    @Log(title = "风控策略", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PayPolicyBo bo, HttpServletResponse response) {
        List<PayPolicyVo> list = payPolicyService.queryList(bo);
        ExcelUtil.exportExcel(list, "风控策略", PayPolicyVo.class, response);
    }

    /**
     * 获取风控策略详细信息
     *
     * @param policyId 主键
     */
    @SaCheckPermission("system:PayPolicy:query")
    @GetMapping("/{policyId}")
    public R<PayPolicyVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long policyId) {
        return R.ok(payPolicyService.queryById(policyId));
    }

    /**
     * 新增风控策略
     */
    @SaCheckPermission("system:PayPolicy:add")
    @Log(title = "风控策略", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PayPolicyBo bo) {
        return payPolicyService.insertByBo(bo);
    }

    /**
     * 修改风控策略
     */
    @SaCheckPermission("system:PayPolicy:edit")
    @Log(title = "风控策略", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PayPolicyBo bo) {
        return payPolicyService.updateByBo(bo);
    }

    /**
     * 删除风控策略
     *
     * @param policyIds 主键串
     */
    @SaCheckPermission("system:PayPolicy:remove")
    @Log(title = "风控策略", businessType = BusinessType.DELETE)
    @DeleteMapping("/{policyIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] policyIds) {
        return toAjax(payPolicyService.deleteWithValidByIds(List.of(policyIds), true));
    }
}
